﻿#include "ChooseGun.h"
#include "ui_choosegun.h"

ChooseGun::ChooseGun(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChooseGun)
{
    ui->setupUi(this);
    count = 0;
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    connect(&Without_operation_timeout, SIGNAL(timeout()), this, SLOT(Come_back_WaitUsePage()));


    Frame_enable[0].Frame = ui->frame_Gun_0;
    Frame_enable[0].Label_0 = ui->gun1_stauts_;
    Frame_enable[0].Label_1 = ui->gun1_stauts;
    Frame_enable[0].PushButton = ui->Choose1;

    Frame_enable[1].Frame = ui->frame_Gun_1;
    Frame_enable[1].Label_0 = ui->gun2_stauts_;
    Frame_enable[1].Label_1 = ui->gun2_stauts;
    Frame_enable[1].PushButton = ui->Choose2;

    Frame_enable[2].Frame = ui->frame_Gun_2;
    Frame_enable[2].Label_0 = ui->gun3_stauts_;
    Frame_enable[2].Label_1 = ui->gun3_stauts;
    Frame_enable[2].PushButton = ui->Choose3;

    Frame_enable[3].Frame = ui->frame_Gun_3;
    Frame_enable[3].Label_0 = ui->gun4_stauts_;
    Frame_enable[3].Label_1 = ui->gun4_stauts;
    Frame_enable[3].PushButton = ui->Choose4;

    Frame_enable[4].Frame = ui->frame_Gun_4;
    Frame_enable[4].Label_0 = ui->gun5_stauts_;
    Frame_enable[4].Label_1 = ui->gun5_stauts;
    Frame_enable[4].PushButton = ui->Choose5;

    Frame_enable[5].Frame = ui->frame_Gun_5;
    Frame_enable[5].Label_0 = ui->gun6_stauts_;
    Frame_enable[5].Label_1 = ui->gun6_stauts;
    Frame_enable[5].PushButton = ui->Choose6;

    Frame_enable[6].Frame = ui->frame_Gun_6;
    Frame_enable[6].Label_0 = ui->gun7_stauts_;
    Frame_enable[6].Label_1 = ui->gun7_stauts;
    Frame_enable[6].PushButton = ui->Choose7;

    Frame_enable[7].Frame = ui->frame_Gun_7;
    Frame_enable[7].Label_0 = ui->gun8_stauts_;
    Frame_enable[7].Label_1 = ui->gun8_stauts;
    Frame_enable[7].PushButton = ui->Choose8;


    for(int i=GUN_AMOUNT; i<8; i++)
    {
        Frame_enable[i].Frame->setStyleSheet("QFrame{background-color:rgb(116,113,115);border-radius: 15px;}");
        Frame_enable[i].Label_0->setStyleSheet("QLabel{background-color:rgb(116,113,115);color:red;}");
        Frame_enable[i].Label_1->setStyleSheet("QLabel{background-color:rgb(116,113,115);color:red;}");
        Frame_enable[i].PushButton->setEnabled(false);
        Frame_enable[i].Label_0->setText(QString(tr("未侦测到")));
        Frame_enable[i].Label_1->setText(QString(tr("未连接")));
    }
#if(GUN_AMOUNT == 1)
    ui->frameMid->hide();
#elif(GUN_AMOUNT == 2)
    for(int i=GUN_AMOUNT; i<8; i++)
    {
        Frame_enable[i].PushButton->hide();
        Frame_enable[i].Label_1->hide();
        Frame_enable[i].Label_0->hide();
        Frame_enable[i].Frame->hide();
    }
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        Frame_enable[i].PushButton->setParent(Frame_enable[i].Frame);
        Frame_enable[i].Label_0->setParent(Frame_enable[i].Frame);
        Frame_enable[i].Label_1->setParent(Frame_enable[i].Frame);
        if(i == 0)
            Frame_enable[i].Frame->setGeometry(90,90,250,150);
        else if(i == 1)
            Frame_enable[1].Frame->setGeometry(430,90,250,150);
        Frame_enable[i].PushButton->setGeometry(0,0,250,150);
        Frame_enable[i].PushButton->setStyleSheet("QPushButton{background-color:transparent;color: rgb(25, 25, 112); font: 30px; font-weight: bold;  border-radius: 10px;} QPushButton:pressed{background-color: qlineargradient(x1:0,x2:0,y1:0,y2:1,stop:0 rgb(126, 205, 244), stop:0.8 rgb(126, 205, 244), stop: 1 rgb(153, 153, 153)); color: rgb(25, 25, 112); font: 20px; border-radius: 15px;}");
        Frame_enable[i].Label_0->setGeometry(0,0,250,30);
        Frame_enable[i].Label_0->setStyleSheet("background-color:transparent;color: rgb(25, 25, 112);font:18px;");
        Frame_enable[i].Label_1->setGeometry(0,120,250,30);
        Frame_enable[i].Label_1->setStyleSheet("background-color:transparent;color: rgb(25, 25, 112);font:18px;");
    }
#elif(GUN_AMOUNT == 4)

#elif(GUN_AMOUNT == 6)

#elif(GUN_AMOUNT == 8)
#endif
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        connect(Frame_enable[i].PushButton, SIGNAL(clicked()), this, SLOT(on_ChooseNum_clicked()));
        connect(&Refreash_Thread[i], SIGNAL(putInCharger(int)), this, SLOT(handle_put_in_charge(int)));
    }

    GunInfo = new QTimer(this);
    connect(GunInfo, SIGNAL(timeout()), this, SLOT(update_Frame()));
    GunInfo->start(1000);
    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(Focus_Gun_Init(int)));
}

ChooseGun::~ChooseGun()
{
    delete ui;
}
void ChooseGun::Focus_Gun_Init(int current)
{
    if(current == ChooseGunPage)
    {
        focus_gun_ = 0;//焦点枪初始化
        Without_operation_timeout.start(1000);
#if (GUN_AMOUNT == 1)
        focus_gun_ = 1;
        emit ChooseGun_No(1);
        qDebug()<<QString("当前焦点:")<<focus_gun_;
#endif
    }
    else if(current != ChooseGunPage)
    {
        Without_operation_timeout.stop();
    }
}
void ChooseGun::Come_back_WaitUsePage()
{
    count++;
    if(count == 80)
    {
        Back_Stand_By();
        count = 0;
    }
}
void ChooseGun::on_ChooseNum_clicked()
{
    QPushButton* btn =  static_cast<QPushButton*>(sender());
    //待完成
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        if(btn == Frame_enable[i].PushButton)
        {
            focus_gun_ = i+1;
            emit ChooseGun_No(focus_gun_);
            qDebug()<<QString("当前焦点:")<<focus_gun_;
        }
    }
}

void ChooseGun::handle_put_in_charge(int gun_no)
{
    if (gun_no < GUN_AMOUNT)
    {
        focus_gun_ = gun_no + 1;
        emit ChooseGun_No(focus_gun_);
    }
}

void ChooseGun::update_Frame()
{
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        setText_gun_stauts(i);
        setText_gun_stauts_(i);
    }
}

void ChooseGun::setText_gun_stauts(int current)
{
    if (chargingData->gunmsg[current].gun_conn_state == 0)//枪已连接
    {
        Frame_enable[current].Label_1->setText(QString(tr("已连接")));
    }
    else
    {
        Frame_enable[current].Label_1->setText(QString(tr("未连接")));
    }
}

void ChooseGun::setText_gun_stauts_(int current)
{
    if(chargingData->gunmsg[current].charge_state == 0)
    {
        Frame_enable[current].Label_0->setText(QString(tr("空闲")));
        if(chargingData->gunmsg[current].errcode[0] != 0 ||
                chargingData->gunmsg[current].meter_state > 0 ||
                chargingData->can_state[current] > 0)//故障标识
        {
            Frame_enable[current].Label_0->setText(QString(tr("故障")));
        }
    }
    if(chargingData->gunmsg[current].charge_state == 1)
    {
        Frame_enable[current].Label_0->setText(QString(tr("充电中")));
    }
    if(chargingData->gunmsg[current].charge_state == 2)
    {
        Frame_enable[current].Label_0->setText(QString(tr("充电结束")));
    }

}
void ChooseGun::on_buttonBack_clicked()
{
    stackLayout->setCurrentIndex(WaitUsePage);
}
